package cn.shenlong.portal.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.shenlong.common.Page;
import cn.shenlong.portal.domain.Member;
import cn.shenlong.portal.domain.MemberCard;
import cn.shenlong.portal.service.MemberCardService;
import cn.shenlong.portal.service.MemberService;

@Controller
@RequestMapping("/memberCard/admin")
public class MemberCardAController {
	@Autowired
	private MemberCardService memberCardService;
	
	@Autowired
	private MemberService memberService;
	
	@RequestMapping("/index")
	public String index(){
		return "/server/memberCard/index";
	}
	
	@RequestMapping("/list")
	public String main(MemberCard memberCard,String searchProperty, String searchValue,String checkStatus,Integer pageSize, Integer pageNumber,ModelMap model){
		if(pageSize==null){
			pageSize = 20;
		}
		if(pageNumber==null){
			pageNumber = 1;
		}
		
		Map<String,Object> parm = new HashMap<String,Object>();
		if(searchProperty!=null){
			parm.put(searchProperty, searchValue);
			model.put("searchValue", searchValue);
			parm.put("searchProperty", searchProperty);
			model.put("searchProperty", searchProperty);
			
			if ("memberName".equals(searchProperty)) {
				Member member = memberService.findMemberByName(searchValue);
				if (member != null) {
					parm.put("memberId", member.getMemberId());
				}
			}
		}
		if (checkStatus != null && !"".equals(checkStatus)) {
			parm.put("checkStatus", checkStatus);
			model.put("checkStatus", checkStatus);
		}
		
		Page<MemberCard> p = memberCardService.findPageByParm(parm, pageNumber, pageSize);
		
		List<MemberCard> lst = (List<MemberCard>) p.getList();
		for (MemberCard mc : lst) {
			if (mc.getMemberId() != null) {
				mc.setMember(memberService.findMemberByMemberId(mc.getMemberId()));
			}
		}
		
		
		model.put("page", p);
		return "/server/memberCard/list";
	}
	
	@RequestMapping("/addMemberCard")
	public String addMemberCard(){
		return "/server/memberCard/add";
	}
	
	@RequestMapping("/showMemberCard")
	public String showMemberCard(Long memberCardId,ModelMap model){		
		MemberCard memberCard = this.memberCardService.findMemberCardByMemberCardId(memberCardId);
		model.put("memberCard", memberCard);
		return "/server/memberCard/show";
	}
	
	@RequestMapping("/saveMemberCard")
	public String saveMemberCard(MemberCard memberCard,ModelMap model,MultipartHttpServletRequest request){
		if(memberCard == null || memberCard.getMemberCardId() == null){ //新增
			try {
				memberCard.setDataflg(1);
				memberCardService.save(memberCard);
				if(memberCard != null) model.put("msg", "success");
				return "redirect:list.html";
			} catch (Exception e) {
				model.put("msg", "failed");
			}
			return addMemberCard();
		} else {//编辑
			try {
				memberCard = memberCardService.updateMemberCard(memberCard);
				if(memberCard != null) model.put("msg", "success");
				return "redirect:list.html";
			} catch (Exception e) {
				model.put("msg", "failed");
				model.put("memberCard", memberCard);
			}
			return this.editMemberCard(memberCard.getMemberCardId(), model);
		}
		
	}
	
	@RequestMapping("/editMemberCard")
	public String editMemberCard(Long memberCardId,ModelMap model){
		if(model == null || model.get("memberCard") == null){			
			MemberCard memberCard = this.memberCardService.findMemberCardByMemberCardId(memberCardId);
			model.put("memberCard", memberCard);
		}
		return "/server/memberCard/edit";
	}
	
	@RequestMapping("/deleteMemberCard")
    @ResponseBody
	public Map<String ,Object> deleteMemberCard(Long memberCardId,String memberCardIdStr,HttpServletRequest request, HttpServletResponse response,ModelMap model){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			if (memberCardId == null) {				
				String[] tmp = memberCardIdStr.split(",");
				Long[] memberCardIds = new Long[tmp.length];
				for (int i = 0; i < tmp.length; i++) {
					memberCardIds[i] = Long.parseLong(tmp[i]);
				}
				memberCardService.deleteMemberCard(memberCardIds);
			}else{
				memberCardService.deleteMemberCard(new Long[]{memberCardId});
			}
			resultMap.put("result", "删除成功！");	
		}catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result","删除失败！原因："+e.toString());
		}
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping(value = "/ajaxMemberCode")
	public Map<String, Object> ajaxMemberCode(String memberCardCode, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		if (StringUtil.isBlank(memberCardCode)) {
			resultMap.put("msg", "参数为空");
			return resultMap;
		}
		
		MemberCard memberCard = memberCardService.findMemberCardByCode(memberCardCode);
		if (memberCard == null) {
			resultMap.put("msg", "true");
		} else {
			resultMap.put("msg", "该卡号已存在，请重新输入");
		}

		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping(value = "/doConfirm")
	public Map<String, Object> doConfirm(Long memberCardId, String status, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		if (StringUtil.isBlank(status)) {
			resultMap.put("msg", "参数为空");
			return resultMap;
		}
		
		MemberCard memberCard = memberCardService.findMemberCardByMemberCardId(memberCardId);
		if (memberCard != null) {
			try {
				if ("true".equals(status)) {
					if (memberCard.getMemberId() != null) {
						memberCard.setIsValid(true);						
						memberCardService.updateMemberCard(memberCard);
					} else {
						resultMap.put("msg", "操作失败，当前会员卡尚未与会员绑定");
						return resultMap;
					}
				} else {
					memberCardService.unbind(memberCard);
				}
				resultMap.put("msg", "true");
			} catch (Exception e) {
				resultMap.put("msg", e.getMessage());
			}
		} else {
			resultMap.put("msg", "参数为空");
			return resultMap;
		}

		return resultMap;
	}
}
